'use strict';

/**
 * Config for the router
 */
angular.module('app')
  .run(
    [          '$rootScope', '$state', '$stateParams',
      function ($rootScope,   $state,   $stateParams) {
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;        
      }
    ]
  )
  .config(
    [          '$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG', 
      function ($stateProvider,   $urlRouterProvider, JQ_CONFIG, MODULE_CONFIG) {
          var base = window.STATIC_BASE_URL;
          var layout = base + "/app/tpl/app.html?v=" + window.__VERSION__;

          $urlRouterProvider
            .otherwise('/login');
          
          $stateProvider
              .state('login', {
                  url: '/login',
                  templateUrl: base + 'login/tpl/index.html?v=' + window.__VERSION__,
                  resolve:load(['toaster', 'ui.select', 'angularFileUpload', 'textAngular', 'daterangepicker', 'smart-table',
                    base + 'login/controllers/index.js?v='+window.__VERSION__])
              })
              .state('app', {
                  abstract: true,
                  url: '/app',
                  templateUrl: layout,
                  resolve:load(['toaster', 'ui.select', 'angularFileUpload', 'textAngular', 'daterangepicker', 'smart-table'])
              })
              .state('app.demand', {
                  url: '/demand',
                  template: '<div ui-view></div>',
              })
              .state('app.demand.list', {
                  url: '/list',
                  templateUrl: base + 'demand/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load([base + 'demand/controllers/list.js?v='+window.__VERSION__])
              })
              .state('app.demand.one', {
                  url: '/one',
                  templateUrl: base + 'demand/tpl/one.html?v=' + window.__VERSION__,
                  resolve: load([base + 'demand/controllers/one.js?v='+window.__VERSION__])
              })
              .state('app.demand.audit', {
                  url: '/audit',
                  templateUrl: base + 'demand/tpl/audit.html?v=' + window.__VERSION__,
                  resolve: load([base + 'demand/controllers/audit.js?v='+window.__VERSION__])
              })
              .state('app.product', {
                  url: '/product',
                  template: '<div ui-view></div>',
              })
              .state('app.product.one', {
                  url: '/one',
                  templateUrl: base + 'product/tpl/one.html?v=' + window.__VERSION__,
                  resolve: load([base + 'product/controllers/one.js?v='+window.__VERSION__])
              })
              .state('app.product.list', {
                  url: '/list',
                  templateUrl: base + 'product/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load([base + 'product/controllers/list.js?v='+window.__VERSION__])
              })
              .state('app.product.audit', {
                  url: '/audit',
                  templateUrl: base + 'product/tpl/audit.html?v=' + window.__VERSION__,
                  resolve: load([base + 'product/controllers/audit.js?v='+window.__VERSION__])
              })
              .state('app.user', {
                  url: '/user',
                  template: '<div ui-view></div>',
              })
              .state('app.user.list', {
                  url: '/list',
                  templateUrl: base + 'user/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'user/controllers/list.js?v='+window.__VERSION__])
              })
              .state('app.user.one', {
                  url: '/one',
                  templateUrl: base + 'user/tpl/one.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'user/controllers/one.js?v='+window.__VERSION__])
              })
              .state('app.tag', {
                  url: '/tag',
                  template: '<div ui-view></div>',
              })
              .state('app.tag.list', {
                  url: '/list',
                  templateUrl: base + 'tag/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'tag/controllers/list.js?v='+window.__VERSION__])
              })
              .state('app.tag.one', {
                  url: '/one',
                  templateUrl: base + 'tag/tpl/one.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'tag/controllers/one.js?v='+window.__VERSION__])
              })
              .state('app.banner', {
                  url: '/banner',
                  template: '<div ui-view></div>',
              })
              .state('app.banner.list', {
                  url: '/list',
                  templateUrl: base + 'banner/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'banner/controllers/list.js?v='+window.__VERSION__])
              })
              .state('app.banner.one', {
                  url: '/one',
                  templateUrl: base + 'banner/tpl/one.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'banner/controllers/one.js?v='+window.__VERSION__])
              })
              .state('app.money', {
                  url: '/money',
                  template: '<div ui-view></div>',
              })
              .state('app.money.list', {
                  url: '/list',
                  templateUrl: base + 'money/tpl/list.html?v=' + window.__VERSION__,
                  resolve: load(['stylecss', base + 'money/controllers/list.js?v='+window.__VERSION__])
              })
              ;

          function load(srcs, callback) {
            return {
                deps: ['$ocLazyLoad', '$q',
                  function( $ocLazyLoad, $q ){
                    var deferred = $q.defer();
                    var promise  = false;
                    srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                    if(!promise){
                      promise = deferred.promise;
                    }
                    angular.forEach(srcs, function(src) {
                      promise = promise.then( function(){
                        if(JQ_CONFIG[src]){
                          return $ocLazyLoad.load(JQ_CONFIG[src]);
                        }
                        angular.forEach(MODULE_CONFIG, function(module) {
                          if( module.name == src){
                            name = module.name;
                          }else{
                            name = src;
                          }
                        });
                        return $ocLazyLoad.load(name);
                      } );
                    });
                    deferred.resolve();
                    return callback ? promise.then(function(){ return callback(); }) : promise;
                }]
            }
          }


      }
    ]
  );
